#include "addmenu.h"
#include "ui_addmenu.h"
#include"QTabWidget"
#include"Init.h"
#include"QColor"
#include<QColorDialog>
#include<QMessageBox>
#include<drawsubway.h>
#include<mainwindow.h>
#include "sqliteoperate.h"
addMenu::addMenu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addMenu)
{
    ui->setupUi(this);
    connect(ui->pushButtonAddLine,&QPushButton::clicked,this,[=]{addLine();});
    connect(ui->pushbuttongetcolor,&QPushButton::clicked,this,[=]{getcolor();});
    connect(ui->pushButtonaddsta,&QPushButton::clicked,this,[=]{addstation();});
    connect(ui->pushButton,&QPushButton::clicked,this,[=]{addedge();});
    //qDebug()<<"错了";
    initcombox1();
   // qDebug()<<"错了";
}

void addMenu::initcombox1(){
    QStringList Line_list,Line_list1;
    for(auto &i:LineColor.keys()){
        Line_list.append(i);
        Line_list1.append(i);
    }
    std::sort(Line_list.begin(),Line_list.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    std::sort(Line_list1.begin(),Line_list1.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
//    ui->comboBox->addItems(Line_list);
//    QLineEdit *line1 = new QLineEdit;
//    ui->comboBox->setLineEdit(line1);
//    ui->comboBox->lineEdit()->clear();

//qDebug()<<"1错误";
    ui->comboBox_4->clear();
    ui->comboBox_4->addItems(Line_list1);
    QLineEdit *line2 = new QLineEdit;
    ui->comboBox_4->setLineEdit(line2);
    ui->comboBox_4->lineEdit()->clear();

//qDebug()<<"2错了";
    QStringList sta_name,sta_name1;
    for(auto &i:Station.keys()){
        sta_name.append(i);
        sta_name1.append(i);
    }
    std::sort(sta_name.begin(),sta_name.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    std::sort(sta_name1.begin(),sta_name1.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    ui->comboBox_2->clear();
    ui->comboBox_2->addItems(sta_name);
    QLineEdit *line3 = new QLineEdit;
    ui->comboBox_2->setLineEdit(line3);
    ui->comboBox_2->lineEdit()->clear();
//qDebug()<<"3错了";
    ui->comboBox_3->clear();
    ui->comboBox_3->addItems(sta_name1);
    QLineEdit *line4 = new QLineEdit;
    ui->comboBox_3->setLineEdit(line4);
    ui->comboBox_3->lineEdit()->clear();


    ui->comboBox_5->clear();
    ui->comboBox_5->addItems(sta_name1);
    QLineEdit *line5 = new QLineEdit;
    ui->comboBox_5->setLineEdit(line5);
    ui->comboBox_5->lineEdit()->clear();
}

void addMenu::addstation(){
    QString staname = ui->lineEdit_2->text();
    if(staname.size()==0){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请输入站点.",QMessageBox::Close);
        MyBox.exec();
        this->flag=0;
        return ;
    }

    QMap<QString, node>::iterator its =Station.find(staname);
    //    for(auto i:LineColor.keys()){
    //        qDebug()<<i<<"\n";
    //    }
    if (its != Station.end()) {
        qDebug()<<"站点已经存在: "<<staname<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点已经存在.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }
    QString dongjing=ui->doubleSpinBox->text();
    QString beiwei=ui->doubleSpinBox_2->text();
    SqliteOperate sqliteOperate;
    int flag=sqliteOperate.insert_station(staname,beiwei,dongjing);
    qDebug()<<"flag"<<flag;
    if(flag==0){
        ui->textBrowser_2->append("站点"+staname+"添加成功");
        
    }
    sqliteOperate.select_all_data_txt();
    infoinit();
    infoaddstation();
    initcombox1();
}

void addMenu::addedge(){
    QString station,p_station,n_station,linename;
    station = ui->comboBox_5->lineEdit()->text();
    p_station = ui->comboBox_2->lineEdit()->text();
    n_station = ui->comboBox_3->lineEdit()->text();
    linename = ui->comboBox_4->lineEdit()->text();


    if(station.size()==0|p_station.size()==0 || n_station.size()==0 ||linename.size()==0){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请输入完整信息.",QMessageBox::Close);
        MyBox.exec();
    }
    QMap<QString, node>::iterator its =Station.find(station);
    if (its == Station.end()) {
        qDebug()<<"站点1不存在: "<<station<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点1不存在.",QMessageBox::Close);
        MyBox.exec();
        return;
    }

    its =Station.find(p_station);
    if (its == Station.end()) {
        qDebug()<<"站点2不存在: "<<p_station<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点2不存在.",QMessageBox::Close);
        MyBox.exec();
        return;
    }

    its =Station.find(n_station);
    if (its == Station.end()) {
        qDebug()<<"站点2不存在: "<<n_station<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点2不存在.",QMessageBox::Close);
        MyBox.exec();
        return;
    }


    QMap<QString, QColor>::iterator it =LineColor.find(linename);
    //    for(auto i:LineColor.keys()){
    //        qDebug()<<i<<"\n";
    //    }
    if (it == LineColor.end()) {
        qDebug()<<"不存在线路: "<<linename<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","请选择存在的线路.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    // 将信息添加到txt文件

//    QFile linefie("../testgitee/test/addedge.txt");
//    /*  addedge.txt
//   station1 station2  linename
//*/
//    linefie.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
//    if(!linefie.isOpen()){
//        qDebug()<<"error open"<<linefie.errorString();
//        this->flag = 0;
//        return ;
//    }
//    QTextStream stream(&linefie);
//    stream.setCodec("UTF-8");
//    if (linefie.size() > 0) {
//        linefie.seek(linefie.size() - 1); // 移动到倒数第二个字符，忽略换行符
//        while (!stream.atEnd()) {
//            QString line = stream.readLine();
//        }
//    }
//    stream<<sta1<<" "<<sta2<<" "<<linename<<endl;
//    linefie.close();
    SqliteOperate sqliteOperate;
    if(sqliteOperate.insert_station_to_line(station,linename,p_station,n_station)==0){
          QMessageBox MyBox(QMessageBox::Information,"信息","成功添加边:"+station+"到"+linename+"",QMessageBox::Close);
          MyBox.exec();
    }
    sqliteOperate.select_all_data_txt();


    infoinit();
    infoaddedge();
    initcombox1();
}

addMenu::~addMenu()
{
    delete ui;
}
void addMenu::getcolor(){
    this->color=QColorDialog::getColor(Qt::white,this);
    ui->pushbuttongetcolor->setStyleSheet(QString("background-color:rgb(%1,%2,%3)").arg(color.red()).arg(color.green()).arg(color.blue()));
    qDebug()<<this->color;
    this->flag = 1;
}

void addMenu::addLine(){
    ui->textBrowser->clear();

    QString name = ui->lineEdit->text();
    if(name.size()==0){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请输入线路名称.",QMessageBox::Close);
        MyBox.exec();
        this->flag=0;
        return ;
    }

    if(!this->flag){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请选择颜色.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return ;
    }

    QMap<QString, QColor>::iterator it =LineColor.find(name);
//    for(auto i:LineColor.keys()){
//        qDebug()<<i<<"\n";
//    }
    if (it != LineColor.end()) {
        qDebug()<<name<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","请不要输入相同线路.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    while(!this->flag){};

    bool exists = false;
    for (const auto pair : LineColor.values()) {
        if (pair == this->color) {
            exists = true;
            break;
        }
    }
    if(exists){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请不要选择已经存在的颜色.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    SqliteOperate sqliteOperate;
    if(sqliteOperate.insert_line(name,color.name())!=-1){
        ui->textBrowser->append("线路"+name+"插入成功"+" 颜色"+this->color.name());
        sqliteOperate.select_all_data_txt();
    }else{
        QMessageBox MyBox(QMessageBox::Warning,"警告","sqlite ERROR!",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }
    infoinit();
    infoaddline();
    initcombox1();

}
